﻿
using System.Data.Linq;
using System.Data.Linq.Mapping;
using BSU.GraduateWork.Data.Model;
using BSU.GraduateWork.Utilities;

namespace BSU.GraduateWork.Data.Repositories
{

    public class GraduateWorkDataContext : DataContext
    {
        #region properties

        public Table<Problem3D> Problems { get { return GetTable<Problem3D>(); } }
        public Table<SolutionPoint3D> Points3D { get { return GetTable<SolutionPoint3D>(); } }
        public Table<SolutionPoint2D> Points2D { get { return GetTable<SolutionPoint2D>(); } }

        #endregion

        #region constructors

        public GraduateWorkDataContext()
            : this(new Configuration().DBConnectionString)
        {
        }

        public GraduateWorkDataContext(DataLoadOptions dlo, bool useDefaultDataLoadingOptions)
            : this(new Configuration().DBConnectionString, dlo, useDefaultDataLoadingOptions)
        {
        }


        public GraduateWorkDataContext(string connectionString)
            : base(connectionString)
        {
            InitializeContext(null, true);
        }

        public GraduateWorkDataContext(string connectionString, DataLoadOptions dlo, bool useDefaultDataLoadingOptions)
            : base(connectionString)
        {
            InitializeContext(dlo, useDefaultDataLoadingOptions);
        }

        public GraduateWorkDataContext(string connectionString, XmlMappingSource map)
            : base(connectionString, map)
        {
            InitializeContext(null, true);
        }

        #endregion

        #region methods

        private void InitializeContext(DataLoadOptions dlo, bool useDefaultDataLoadingOptions)
        {
            if (dlo == null) dlo = new DataLoadOptions();
            if (useDefaultDataLoadingOptions)
            {
                dlo.LoadWith<Problem3D>(x => x.Solution);
                dlo.LoadWith<Problem3D>(x => x.InputParameters);

                dlo.LoadWith<Problem2D>(x => x.Solution);
                dlo.LoadWith<Problem2D>(x => x.ExactSolution);
                dlo.LoadWith<Problem2D>(x => x.InputParameters);
            }
            LoadOptions = dlo;
        }

        //public override void SubmitChanges(ConflictMode failureMode)
        //{
        //    using (new LinqToSQLProfiller(this, true))
        //    {
        //        base.SubmitChanges(failureMode);
        //    }

        //}

        //protected internal new IExecuteResult ExecuteMethodCall(object instance, MethodInfo methodInfo, params object[] parameters)
        //{
        //    using (new LinqToSQLProfiller(this, true))
        //    {
        //        return base.ExecuteMethodCall(instance, methodInfo, parameters);
        //    }
        //}

        #endregion
    }
}
